<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\model\api;

use laytp\BaseModel;
use think\model\concern\SoftDelete;

class SkyDiyGoods extends BaseModel
{
    use SoftDelete;
    protected $name = 'sky_diy_goods';

    /**
     * 图鉴自定义模块
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function guide(){
        $good_info = (new SkyGoods())->info();

        //查询模块关联物品
        $model_relation_goods = [];
        $model_good_list = $this->cache(true)->order('id desc')->select()->toArray();
        foreach($model_good_list as $item){
            $goods = explode(',',$item['relation_goods']);
            foreach($goods as $good_id){
                $model_relation_goods[$item['model_id']][] = array(
                    'name' => $good_info[$good_id]['name'],
                    'icon' => $good_info[$good_id]['icon'],
                    'ancestor_id' => $good_info[$good_id]['ancestor_id'],
                    'tag'    => $item['tag_name'],
                    'remark'    => $good_info[$good_id]['remark'],
                    'images' => $good_info[$good_id]['relation_img']
                );
            }
        }

        //模块匹配
        $return = [];
        $guide_list = (new SkyDiy())->where('status','=',1)->cache(true)->order('sort desc')
            ->select()->toArray();
        foreach($guide_list as $value){
            $return[] = array(
                'title' => $value['model_name'],
                'type'  => 2,
                'goods' => !empty($model_relation_goods[$value['id']])?$model_relation_goods[$value['id']]:[]
            );
        }
        return $return;
    }
}